// miniprogram/pages/home/publish/publish.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTopTips: false,
    msg: "",
    schoolName: null,
    assoName: null,
    collegeName: null,
    assoType: null,
    stjj: '', //正文内容
    stjjCount: 0, //正文字数
    stxq: '',
    stxqCount: 0,
    files: [],
    depList: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  stjjInput: function handleInput(e) {
    this.setData({
      stjj: e.detail.value,
      stjjCount: e.detail.value.length
    })
  },
  stxqInput: function handleInput(e) {
    this.setData({
      stxq: e.detail.value,
      stxqCount: e.detail.value.length
    })
  },
  // 社团部门
  depNameInput: function(e) {
    var that = this;
    var lists = that.data.depList;
    lists[e.currentTarget.dataset.id].depName = e.detail.value;
    lists[e.currentTarget.dataset.id].depNameCount = e.detail.value.length
    that.setData({
      depList: lists
    })
  },
  depDescInput: function(e) {
    var that = this;
    var lists = that.data.depList;
    lists[e.currentTarget.dataset.id].depDesc = e.detail.value;
    lists[e.currentTarget.dataset.id].depDescCount = e.detail.value.length
    that.setData({
      depList: lists
    })
  },
  addDep: function() {
    var that = this;
    var lists = that.data.depList
    lists.push({
      depName: "",
      depNameCount: 0,
      depDesc: "",
      depDescCount: 0,
    })
    that.setData({
      depList: lists
    })
  },
  deleteDep: function(e) {
    var id = e.currentTarget.dataset.id
    var lists = this.data.depList
    lists.splice(id, 1);
    this.setData({
      depList: lists
    })
  },
  // 预览招聘信息
  formSubmit: function(e) {
    var msg = ""
    var that = this
    let {
      schoolName,
      collegeName,
      assoName,
      assoType
    } = e.detail.value;
    var depList = this.data.depList
    var depIsNull = false
    if (depList.length != 0) {
      for (var i = 0; i < depList.length; i++) {
        if (depList[i].depNameCount == 0 || depList[i].depDescCount == 0) {
          depIsNull = true;
          break;
        }
      }
    }
    if (this.data.stjjCount == 0 || this.data.stxqCount == 0||this.data.files.length==0) {
      if (this.data.stjjCount == 0) {
        msg = msg + "社团简介 "
      }
      if (this.data.stxqCount == 0) {
        msg = msg + "社团详情 "
      }
      if (this.data.files.length == 0) {
        msg = msg + "社团照片 "
      }
      this.setData({
        msg: msg + "不能为空"
      })
      this.setData({
        showTopTips: true
      });
      setTimeout(function() {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (depIsNull) {
      this.setData({
        msg: msg + "若添加了社团部门，部门名或部门简介不能为空"
      })
      this.setData({
        showTopTips: true
      });
      setTimeout(function() {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {
      var files = JSON.stringify(this.data.files)
      depList = JSON.stringify(depList)
      wx.navigateTo({
        url: './preview?schoolName=' + schoolName +
          '&collegeName=' + collegeName +
          '&assoName=' + assoName +
          '&assoType=' + assoType +
          '&stjj=' + this.data.stjj +
          '&stxq=' + this.data.stxq +
          '&stzps=' + files +
          '&depList=' + depList
      })
    }
  },
  previewImage: function(e) {
    console.log(e.currentTarget.id)
    console.log(this.data.files)

    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  deleteImage: function (e) {
    var that = this
    console.log(e)
    var index = parseInt(e.currentTarget.dataset.index);
    that.setData({
      files: that.data.files.splice(index + 1, 1)
    })
  },
  chooseImage: function(e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var studentInfo = app.globalData.studentInfo
    this.setData({
      schoolName: studentInfo.schoolName
    })
    wx.request({
      url: app.globalData.url + '/asso/getAssoByStuNo',
      data: {
        uniqueId:app.globalData.uniqueId
      },
      success: res => {
        var assoInfo = res.data.assoInfo
        app.globalData.assoInfo = assoInfo
        this.setData({
          assoName: assoInfo.assoName,
          collegeName: assoInfo.collegeName,
          assoType: assoInfo.assoType,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})